

local MakePlayerCharacter = require 'prefabs/player_common'

local avatar_name = 'kukude'
local assets = {
	Asset('SCRIPT', 'scripts/prefabs/player_common.lua'),
	Asset('ANIM', 'anim/'..avatar_name..'.zip'),
	Asset('ANIM', 'anim/ghost_'..avatar_name..'_build.zip'),
}

local prefabs = {}

local start_inv = {}
-- for k, v in pairs(TUNING.GAMEMODE_STARTING_ITEMS) do
-- 	start_inv[string.lower(k)] = v[string.upper(avatar_name)]
-- end
start_inv['default'] = {}
for k,v in pairs(TUNING.KUKUDE_CUSTOM_START_INV) do
	for i = 1, v.num do 
		table.insert(start_inv['default'], k)
	end
end

prefabs = FlattenTree({ prefabs, start_inv }, true)
---------------------------------------------------------------------------
---------------------------------------------------------------------------
local function onbecamehuman(inst, data, isloading)
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, avatar_name..'_speed_mod', 1)
	local fleshsack = inst.components.inventory and inst.components.inventory:GetEquippedItem(EQUIPSLOTS.KKD_FLESH)
	if fleshsack == nil then
		fleshsack = data and data.kkd_fleshsack and SpawnSaveRecord(data.kkd_fleshsack, isloading) or SpawnPrefab("kkd_fleshsack")
		if fleshsack ~= nil then 
			inst.components.inventory:Equip(fleshsack)
		end
	end
end

local function onbecameghost(inst, data)
	inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, avatar_name..'_speed_mod')
	local fleshsack = inst.components.inventory and inst.components.inventory:GetEquippedItem(EQUIPSLOTS.KKD_FLESH)
	if fleshsack ~= nil and fleshsack.components.container ~= nil then
		for i = 1, fleshsack.components.container.numslots do
			local insert_obj = fleshsack.components.container:GetItemInSlot(i)
			if insert_obj ~= nil and insert_obj.components.finiteuses ~= nil then
				insert_obj.components.finiteuses:Use(TUNING.MOD_KUKUDE.IO.FINITEUSE)
			end
		end
		fleshsack.components.container:DropEverything()
		fleshsack:Remove()
	end
end
---------------------------------------------------------------------------
---------------------------------------------------------------------------
---comment
---@param inst ent
---@param data any
local function onload(inst,data, ...)
	inst:ListenForEvent('ms_respawnedfromghost', onbecamehuman)
	inst:ListenForEvent('ms_becameghost', onbecameghost)
    inst:ListenForEvent("ms_playerreroll", onbecameghost)

	if inst:HasTag('playerghost') then
		onbecameghost(inst)
	else
		onbecamehuman(inst, data, ...)
	end

	local fleshsack = inst.components.inventory and inst.components.inventory:GetEquippedItem(EQUIPSLOTS.KKD_FLESH)
	if not fleshsack then return end 

	if fleshsack.onequipdirty then 
		for i = 1, #fleshsack.onequipdirty do 
			fleshsack.onequipdirty[i].fn(fleshsack.onequipdirty[i].inst, fleshsack, inst)
		end
		fleshsack.onequipdirty = nil 
	end
	--[[if data then
		if data.kkd_currenthealth then
			inst.kkd_currenthealth = data.kkd_currenthealth
			c_announce("inst.kkd_currenthealth = "..inst.kkd_currenthealth)
		else
			inst.kkd_currenthealth = nil
		end
		if data.kkd_currenthunger then
			inst.kkd_currenthunger = data.kkd_currenthunger
			c_announce("inst.kkd_currenthunger = "..inst.kkd_currenthunger)
		else
			inst.kkd_currenthunger = nil
		end
		if data.kkd_currentsanity then
			inst.kkd_currentsanity = data.kkd_currentsanity
			c_announce("inst.kkd_currentsanity = "..inst.kkd_currentsanity)
		else
			inst.kkd_currentsanity = nil
		end
	end]]
end

---comment
---@param inst ent
---@param data any
local function onsave(inst, data)
	--[[data.kkd_currenthealth = inst.components.health.currenthealth
	data.kkd_currenthunger = inst.components.hunger.current
	data.kkd_currentsanity = inst.components.sanity.current]]

	if data.inventory and data.inventory.equip and data.inventory.equip[EQUIPSLOTS.KKD_FLESH] then 
		data.kkd_fleshsack = deepcopy(data.inventory.equip[EQUIPSLOTS.KKD_FLESH])
		data.inventory.equip[EQUIPSLOTS.KKD_FLESH] = nil
	end
	return data
end

local function ondespawn(inst)
	inst:PushEvent("kkd_despawn")
end
---------------------------------------------------------------------------
---------------------------------------------------------------------------
-- 主/客机
local common_postinit = function(inst)
	inst:AddTag(avatar_name)
	
	inst.MiniMapEntity:SetIcon(avatar_name..'.tex')
	inst.attackspeedmult = net_float(inst.GUID, "attackspeedmult","onatkspeeddirty")
	inst.attackspeedmult:set(1)
end
-- 主机
local master_postinit = function(inst)	
	inst.starting_inventory = start_inv[TheNet:GetServerGameMode()] or start_inv.default
	inst.soundsname = 'wendy'
	
	inst.components.health:SetMaxHealth(TUNING[string.upper(avatar_name)..'_HEALTH'])
	inst.components.hunger:SetMax(TUNING[string.upper(avatar_name)..'_HUNGER'])
	inst.components.sanity:SetMax(TUNING[string.upper(avatar_name)..'_SANITY'])

	inst.OnSave = onsave
	inst.OnPreLoad = onload
	--inst.OnLoad = onload
	inst.OnNewSpawn = onload
	inst.OnDespawn = ondespawn
end
-- 人物皮肤
local function MakeKUKUDESkin(name, data, notemp, free)
	local d = {}
	d.rarity = '典藏'
	d.rarityorder = 2
	d.raritycorlor = { 0 / 255, 255 / 255, 249 / 255, 1 }
	d.release_group = -1001
	d.skin_tags = { 'BASE', avatar_name, 'CHARACTER' }
	d.skins = {
		normal_skin = name,
		ghost_skin = 'ghost_'..avatar_name..'_build'
	}
	if not free then
		d.checkfn = KUKUDE_API.KUKUDESkinCheckFn
		d.checkclientfn = KUKUDE_API.KUKUDESkinCheckFn
	end
	d.share_bigportrait_name = avatar_name
	d.FrameSymbol = 'Reward'
	for k, v in pairs(data) do
		d[k] = v
	end
	KUKUDE_API.MakeCharacterSkin(avatar_name, name, d)
	if not notemp then
		local d2 = shallowcopy(d)
		d2.rarity = '限时体验'
		d2.rarityorder = 80
		d2.raritycorlor = { 0.957, 0.769, 0.188, 1 }
		d2.FrameSymbol = 'heirloom'
		d2.name = data.name .. '(限时)'
		KUKUDE_API.MakeCharacterSkin(avatar_name, name .. '_tmp', d2)
	end
end
function MakeKUKUDEFreeSkin(name, data)
	MakeKUKUDESkin(name, data, true, true)
end

MakeKUKUDEFreeSkin(avatar_name..'_none', {
	name = '血肉艺术家', -- 皮肤的名称
	des = '*小心它的手术刀，更要小心它用来拿着手术刀的……东西*', -- 皮肤界面的描述
	quotes = '\'加入光荣的进化吧\'', -- 选人界面的描述
	rarity = '典藏', -- 珍惜度 官方不存在的珍惜度则直接覆盖字符串
	rarityorder = 2, -- 珍惜度的排序 用于按优先级排序 基本没啥用
	raritycorlor = { 189 / 255, 73 / 255, 73 / 255, 1 }, -- {R,G,B,A}
	skins = { normal_skin = avatar_name, ghost_skin = 'ghost_'..avatar_name..'_build' },
	build_name_override = avatar_name,
	share_bigportrait_name = avatar_name..'_none',
})


return MakePlayerCharacter(avatar_name, prefabs, assets, common_postinit, master_postinit, prefabs)


